package com.example.Cairodio;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author Shokry
 * 
 * This class creates and upgrades the database
 *
 */
public class DatabaseHelper extends SQLiteOpenHelper {

	static final String dbName = "cairodioDB";
	static  int dbVerion=1 ;
	static final String placesTable = "Places";
	static final String colPlaceID = "_id";
	static final String colName = "name";
	static final String colLat = "lat";
	static final String colLong = "long";

	static final String audioClipsTable = "AudioClips";
	static final String colAudioClipID = "_id";
	static final String colURL = "URL";
	static final String colForeign = "place_id";
	
	static final String commentsTable = "Comments";
	static final String colCommentID = "_id";
	static final String colComment = "Comment";
	static final String colDate = "Date";
	static final String colForeignKey = "place_id";
	
	public DatabaseHelper(Context context) {
		super(context, dbName, null, dbVerion);
		Log.e("Entered Constructor",">>>>>>>>>>>>>>>>>>>>> Initializing Database");
	}

	/**
	 * @author Shokry
	 * 
	 * Create two tables, Places and AudioClips
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
		db.execSQL("CREATE TABLE " + placesTable + " (" + colPlaceID
				+ " INTEGER PRIMARY KEY , " + colName + " TEXT," + colLat
				+ " TEXT, " + colLong + " TEXT)");
		
		Log.e("On Create",">>>>>>>>>>>>>>>>>>>>> Created Places Table");

		db.execSQL("CREATE TABLE " + audioClipsTable + " (" + colAudioClipID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + colURL + " TEXT, "
				+ colForeign  + " INTEGER NOT NULL ,FOREIGN KEY (" + colForeign
				+ ") REFERENCES " + placesTable + " (" + colPlaceID + "));");
		
		Log.e("On Create",">>>>>>>>>>>>>>>>>>>>> Created audioClips Table");
		
		db.execSQL("CREATE TABLE " + commentsTable + " (" + colCommentID
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + colComment + " TEXT, "
				+ colDate + " DATE, " + colForeignKey  + " INTEGER NOT NULL ,FOREIGN KEY (" + colForeignKey
				+ ") REFERENCES " + placesTable + " (" + colPlaceID + "));");
		
		Log.e("On Create",">>>>>>>>>>>>>>>>>>>>> Created Comments Table");

		Log.e("On Create",">>>>>>>>>>>>>>>>>>>>> Created Database Tables");
	}

	

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
	/*	db.execSQL("DROP TABLE IF EXISTS " + placesTable);
		db.execSQL("DROP TABLE IF EXISTS " + audioClipsTable);
		db.execSQL("DROP TABLE IF EXISTS " + commentsTable);
		
		onCreate(db);*/
		
		Log.e("On Upgrade","Upgraded");
	}

}
